﻿using System;
using System.Collections.Specialized;
using System.Configuration;

namespace Cosoware.Web.Navigation
{
    public partial class CalendarSiteMapProvider
    {
        /// <summary>
        /// Name of the provider.
        /// </summary>
        private string _name;

        /// <summary>
        /// Base base Url used as root key
        /// </summary>
        private string _urlBase;

        /// <summary>
        /// Base Url used as key
        /// </summary>
        private string _urlPattern;

        /// <summary>
        /// Base regex used as key
        /// </summary>
        private string _regexPattern;

        /// <summary>
        /// Documents category
        /// </summary>
        private string _category;

        /// <summary>
        /// The year's title date pattern.
        /// </summary>
        private string _yearPattern = "yyyy";

        /// <summary>
        /// The month's title date pattern.
        /// </summary>
        private string _monthPattern = "MMM";
        
        /// <summary>
        /// The day's title date pattern.
        /// </summary>
        private string _dayPattern = "dd";

        /// <summary>
        /// Set first letter to upper case and other to lower case
        /// </summary>
        private bool _monthUpperFirstLetter = false;

        /// <summary>
        /// Initializes the <see cref="T:System.Web.SiteMapProvider"/> implementation, including any resources that are needed to load site map data from persistent storage.
        /// </summary>
        /// <param name="name">The <see cref="P:System.Configuration.Provider.ProviderBase.Name"/> of the provider to initialize.</param>
        /// <param name="attributes">A <see cref="T:System.Collections.Specialized.NameValueCollection"/> that can contain additional attributes to help initialize the provider. These attributes are read from the site map provider configuration in the Web.config file.</param>
        public override void Initialize(string name, NameValueCollection attributes)
        {
            lock (this)
            {
                base.Initialize(name, attributes);
                InitializeName(name);
                InitializeAttributes(attributes);
                CreateSiteMap();
            }
        }

        /// <summary>
        /// Initializes the name.
        /// </summary>
        /// <param name="name">The name.</param>
        private void InitializeName(string name)
        {
            _name = name;
        }

        /// <summary>
        /// Initializes the attributes.
        /// </summary>
        private void InitializeAttributes(NameValueCollection attributes)
        {
            if (attributes["title"] == null)
                throw new InvalidOperationException(String.Format("A 'title' parameter is missed in '{0}' provider configuration.", _name));
            if (attributes["category"] == null)
                throw new InvalidOperationException(String.Format("A 'category' parameter is missed in '{0}' provider configuration.", _name));
            if (attributes["urlPattern"] == null)
                throw new InvalidOperationException(String.Format("An 'urlPattern' parameter is missed in '{0}' provider configuration.", _name));
            if (attributes["urlBase"] == null)
                throw new InvalidOperationException(String.Format("An 'urlBase' parameter is missed in '{0}' provider configuration.", _name));
            if (attributes["regexPattern"] == null)
                throw new InvalidOperationException(String.Format("A 'regexPattern' parameter is missed in '{0}' provider configuration.", _name));
            Title = attributes["title"];
            _category = attributes["category"];
            _urlPattern = ApplicationPath + "/" + attributes["urlPattern"];
            _urlBase = ApplicationPath + "/" + attributes["urlBase"];
            _regexPattern = attributes["regexPattern"];
            if (!String.IsNullOrEmpty(attributes["yearPattern"]))
                _yearPattern = attributes["yearPattern"];
            if (!String.IsNullOrEmpty(attributes["monthPattern"]))
                _monthPattern = attributes["monthPattern"];
            if (!String.IsNullOrEmpty(attributes["dayPattern"]))
                _dayPattern = attributes["dayPattern"];
            if (!String.IsNullOrEmpty(attributes["monthUpperFirstLetter"]))
                Boolean.TryParse(attributes["monthUpperFirstLetter"], out _monthUpperFirstLetter);
        }
    }
}
